<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){

    	if(!$_GET['battle_id']) $this->redirect('Index/login');
        
        $battle_id   = (int)$_GET['battle_id'];
    	$battle_info = M('Wxapp_battle')->where('id = '.$battle_id)->find();
    	
    	$player1_score[0] = $battle_info['player1_score'] < 10 ? 0 : $battle_info['player1_score']{0}; 
    	$player1_score[1] = $battle_info['player1_score'] < 10 ? $battle_info['player1_score']   : $battle_info['player1_score']{1};
        $player2_score[0] = $battle_info['player2_score'] < 10 ? 0 : $battle_info['player2_score']{0}; 
    	$player2_score[1] = $battle_info['player2_score'] < 10 ? $battle_info['player2_score']   : $battle_info['player2_score']{1};
    	//获取赛事名称
    	$battle_info['event_name'] = M('Event')->where('id = '.$battle_info['event_id'])->getField('event_name');
        
        $this->assign('battle_id',$battle_id);
    	$this->assign('battle_info',$battle_info);
    	$this->assign('player1_score',$player1_score);
    	$this->assign('player2_score',$player2_score);
    	$this->display();
    }
    public function login(){
        $this->assign('login_code',getRandCode(10));
    	$this->display();
    }

    public function createQRcode(){
        Vendor('phpqrcode.phpqrcode');
        //生成二维码图片
        $object     = new \QRcode();
        $login_code = I('get.code');//网址或者是文本内容
     
        $object->png($login_code, false, intval(3), intval(5), 2);
    }

    //监听扫码
    public function monitorDoScanCode(){
    	set_time_limit(0);
    	$where['login_code'] = I('get.login_code');
    	$time       = time();
    	$Battle     = M('Wxapp_battle');
        while (true) {
        	$result = $Battle->where($where)->find();
        	if($result && (time()-$time<30)){
        		$data['state']     = 1;
        		$data['battle_id'] = $result['id'];
        		$data['info']      = '连接成功';
        		break;
        	}else if(time()-$time > 30){
        		$data['state']     = 0;
        		$data['info']      = '等待超时,请重新发起请求';
        		break;
        	}
          	sleep(1);
        }  

        $this->ajaxReturn($data);die;
    }

    //更新计分板比分
    public function updateScoreBoard(){
    	set_time_limit(0);
    	$Battle    = M('Wxapp_battle');
   
    	$battle_id   = (int)$_GET['battle_id'];
        $update_time = (int)$_GET['update_time'];
    	
    	$time       = time();

        while (true) {
        	$battle_info = $Battle->where('id = '.$battle_id)->find();
    
        	if($battle_info['update_time'] != $update_time && (time()-$time<25)){
        		$data['state']         = 1;
        		$data['battle_info']   = $battle_info;
        		break;
        	}else if(time()-$time > 25){
        		$data['state']     = 0;
        		$data['info']      = '请重新发起请求';
        		break;
        	}
          	sleep(1);
        }  

        $this->ajaxReturn($data);die;
    }

    // //更新计分板对局信息
    // public function updateBattleInfo(){
    //     set_time_limit(0);
    //     $Battle   = M('Wxapp_battle');
   
    //     $battle_id     = (int)$_GET['battle_id'];
    //     $update_time   = (int)$_GET['update_time']; 

    //     $time          = time();

    //     while (true) {
    //         $battle_info = $Battle->where('id = '.$battle_id)->find();
    
    //         if(($battle_info['update_time'] !=$update_time) && (time()-$time<25)){
    //             $data['state']         = 1;
    //             $data['battle_info']   = $battle_info;
    //             break;
    //         }else if(time()-$time > 25){
    //             $data['state']     = 0;
    //             $data['info']      = '请重新发起请求';
    //             break;
    //         }
    //         sleep(1);
    //     }  
    //     $this->ajaxReturn($data);die;
    // }
}